/**
 * 主角逻辑脚本
 * 
 * 主要判断主角的碰撞逻辑
 */

var com = require('Common');

cc.Class({
    extends: cc.Component,

    properties: {
        score:0, // 分数
        coinNum:0, // 金币
    },

    onLoad () {
        this.score = cc.sys.localStorage.getItem("score");
        this.coinNum = cc.sys.localStorage.getItem("coinNum");
        // 点击结束后,开启碰撞
        this.node.parent.on(cc.Node.EventType.TOUCH_END,function(event){
            var manager = cc.director.getCollisionManager();
            if (manager.enabled == false) {
                manager.enabled = true;
            }
        },this)
    },

    // 开始碰撞
    onCollisionEnter: function (other, self) {

        //tag为0代表碰撞到的是正常的台阶
        if (other.tag == 0) {
            //把分数存储到本地
            this.score++;
            cc.sys.localStorage.setItem("score",this.score);

            //find为查找场景下对应的组件，getComponent为对应的组件类型
            //获取在Canvas上的分数label来更新分数
            var scoreLabel = cc.find("Canvas/GameView/scoreLabel").getComponent(cc.Label);
            if (scoreLabel) {
                scoreLabel.string = this.score;
            }
        } else if (other.tag == 3) { // tag为3代表碰撞到的是金币
            this.coinNum = parseInt(this.coinNum) + 2;
            cc.sys.localStorage.setItem("coinNum", this.coinNum);
        } else { // 其他皆为碰撞障碍物
            var manager = cc.director.getCollisionManager();
            manager.enabled = false;
            com.gameIsStart = false;
            // 碰撞障碍后,加载结束界面
            var game = this.node.parent.getComponent("Game");
            var outAction = cc.fadeOut(0.1,1.0);
            this.node.runAction(outAction);
            this.node.active = false;
            game.gameIsOver()
        }
    },

});
